package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.mapper.OrderSettingMapper;
import com.itcast.pojo.OrderSetting;
import com.itcast.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;
    //添加
    @Override
    public void add(List<OrderSetting> date) {
        if (date!=null && date.size()>0){
            for (OrderSetting orderSetting : date) {
                //判断当前日期是否已经进行了预约设置
                long countByOrderData = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if (countByOrderData > 0){
                    //已经进行了预约设置，执行更新操作
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                }else {
                    //没有进行预约设置，执行添加操作
                    orderSettingMapper.add(orderSetting);
                }
            }
        }
    }
    //获取预约数据
    @Override
    public List<Map> getOrderSettingMonth(String date) {
        //拼接字符串
        String begin = date + "-1";//2020-1-1
        String end = date + "-31";//2020-1-31
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);

        //调用mapper，根据日期范围回去可预约数据
        List<OrderSetting> list = orderSettingMapper.getOrderSettingMonth(map);
        List<Map> result = new ArrayList<>();
        //判断list是否为空
        if (list!=null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> m = new HashMap<>();
                m.put("date",orderSetting.getOrderDate().getDate());
                m.put("number",orderSetting.getNumber());
                m.put("reservations",orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    //设置预约信息
    @Override
    public void editNumber(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        //根据日期查询是否已经进行了预约设置
        long  count = orderSettingMapper.findCountByOrderDate(orderDate);
        if (count>0){
            //当前日期已经进行了预约设置，需要进行更新操作
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        }else {
            //当前日期没有进行预约设置，需要进行插入操作
            orderSettingMapper.add(orderSetting);
        }
    }
}
